﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class CheLiangBianJi : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;
        private bool m_IsRuKu = false;

        public CheLiangBianJi(bool IsRuKu)
        {
            InitializeComponent();
            m_IsRuKu = IsRuKu;
        }

        public CheLiangBianJi(DataGridViewRow SelectRow,bool IsRuKu)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
            m_IsRuKu = IsRuKu;
        }


        private void CheLiangBianJi_Load(object sender, EventArgs e)
        {
            if (m_IsRuKu)
            {
                cmb_cheliangleixing.SelectedIndex = 0;
            }
            else
            {
                cmb_cheliangleixing.SelectedIndex = 1;
            }
            
            if (m_SelectRow != null)
            {
                txt_chepaihao.Text = string.Format("{0}",m_SelectRow.Cells["col_chepaihao"].Value);
                txt_chepizhong.Text = string.Format("{0}", m_SelectRow.Cells["col_cheliangpizhong"].Value);
                txt_ICkahao.Text = string.Format("{0}",m_SelectRow.Cells["col_chekahao"].Value);
                chk_cheliangzhuangtai.Checked = string.Format("{0}", m_SelectRow.Cells["col_cheliangzhuangtai"].Value) == "启用" ? true : false;
                chk_zidongchengzhong.Checked = string.Format("{0}", m_SelectRow.Cells["col_zidongchengzhong"].Value) == "启用" ? true : false;
                cmb_cheliangleixing.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_cheliangleixing"].Value);
            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txt_chepaihao.Text))
            {
                KeaoUtils.ShowInfoMessage("车牌号不能为空，请重试。");
                return;
            }
            decimal cheLiangPiZhong = 0m;
            if (!string.IsNullOrEmpty(txt_chepizhong.Text)&&KeaoUtils.isNumber(txt_chepizhong.Text))
            {
                cheLiangPiZhong = decimal.Parse(txt_chepizhong.Text);
            }

            if (m_SelectRow == null)
            {
                try
                {
                    string CreateAndCondition = KeaoUtils._DB.CreateAndCondition("chepaihao", DataValueCommonType.字符串, txt_chepaihao.Text, "", SearchConditionType.等于);
                    CreateAndCondition += KeaoUtils._DB.CreateAndCondition("cheliangleixing", DataValueCommonType.字符串, cmb_cheliangleixing.Text, "", SearchConditionType.等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", CreateAndCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("车牌号重复，请重试。");
                        return;
                    }
                    if (!string.IsNullOrEmpty(txt_ICkahao.Text))
                    {
                        CreateAndCondition = KeaoUtils._DB.CreateAndCondition("chekahao", DataValueCommonType.字符串, txt_ICkahao.Text, "", SearchConditionType.等于);
                        Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", CreateAndCondition);
                        if (Dt == null)
                        {
                            return;
                        }
                        if (Dt.Rows.Count > 0)
                        {
                            KeaoUtils.ShowInfoMessage("车辆IC卡已经与其他车辆绑定，请重试。");
                            return;
                        }
                    }

                    TableRow newRow = new TableRow("tbl_cheliang", KeaoUtils.GetGUID(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "chepaihao", ValueMember = txt_chepaihao.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "cheliangpizhong", ValueMember = cheLiangPiZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "chekahao", ValueMember = txt_ICkahao.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zidongchengzhong", ValueMember = chk_zidongchengzhong.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "cheliangleixing", ValueMember = cmb_cheliangleixing.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "cheliangzhuangtai", ValueMember = chk_cheliangzhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                catch(Exception err)
                {
                    KeaoUtils.LogTxt(string.Format("新增车辆信息异常。{0}", err.Message));
                }
            }
            else
            {
                try
                {
                    string createCondition = KeaoUtils._DB.CreateAndCondition("chepaihao", DataValueCommonType.字符串, txt_chepaihao.Text, "", SearchConditionType.等于);
                    createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", createCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("该车牌已经存在不能重复保存，请重试。");
                        return;
                    }
                    if (!string.IsNullOrEmpty(txt_ICkahao.Text))
                    {
                        createCondition = KeaoUtils._DB.CreateAndCondition("chekahao", DataValueCommonType.字符串, txt_ICkahao.Text, "", SearchConditionType.等于);
                        createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                        Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", createCondition);
                        if (Dt == null)
                        {
                            return;
                        }
                        if (Dt.Rows.Count > 0)
                        {
                            KeaoUtils.ShowInfoMessage("车辆IC卡已经与其他车辆绑定，请重试。");
                            return;
                        }
                    }

                    TableRow editRow = new TableRow("tbl_cheliang", string.Format("{0}", m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "chepaihao", ValueMember = txt_chepaihao.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "cheliangpizhong", ValueMember = cheLiangPiZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "chekahao", ValueMember = txt_ICkahao.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "zidongchengzhong", ValueMember = chk_zidongchengzhong.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "cheliangleixing", ValueMember = cmb_cheliangleixing.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "cheliangzhuangtai", ValueMember = chk_cheliangzhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception err)
                {
                    KeaoUtils.LogTxt(string.Format("修改车辆信息异常。#{0}", err.Message));
                }

            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_chehao_Click(object sender, EventArgs e)
        {
            CheLiangPaiHao chePaiHao = new CheLiangPaiHao();
            if (chePaiHao.ShowDialog() == DialogResult.OK)
            {
                txt_chepaihao.Text = chePaiHao.m_Current;
            }
        }

        private void txt_chepizhong_Click(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;
            t.Focus();
            t.SelectAll();
        }
    }
}
